<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理后台登录 - 博物馆预约系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .login-container {
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            width: 100%;
            max-width: 400px;
            margin: 20px;
        }
        
        .login-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 40px 30px;
            text-align: center;
        }
        
        .login-title {
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 8px;
        }
        
        .login-subtitle {
            font-size: 16px;
            opacity: 0.9;
        }
        
        .login-form {
            padding: 40px 30px;
        }
        
        .form-group {
            margin-bottom: 24px;
        }
        
        .form-label {
            display: block;
            font-size: 14px;
            font-weight: 500;
            color: #555;
            margin-bottom: 8px;
        }
        
        .form-input {
            width: 100%;
            height: 48px;
            padding: 0 16px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
            background-color: #f8f9fa;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #667eea;
            background-color: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .password-input {
            position: relative;
        }
        
        .password-toggle {
            position: absolute;
            right: 16px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: #888;
            cursor: pointer;
            font-size: 18px;
        }
        
        .password-toggle:hover {
            color: #555;
        }
        
        .remember-me {
            display: flex;
            align-items: center;
            margin-bottom: 24px;
        }
        
        .checkbox {
            width: 18px;
            height: 18px;
            margin-right: 8px;
            accent-color: #667eea;
        }
        
        .checkbox-label {
            font-size: 14px;
            color: #666;
            cursor: pointer;
        }
        
        .login-button {
            width: 100%;
            height: 48px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }
        
        .login-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
        }
        
        .login-button:disabled {
            background: #ccc;
            transform: none;
            box-shadow: none;
            cursor: not-allowed;
        }
        
        .alert {
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .alert-error {
            background-color: #fee;
            border: 1px solid #fcc;
            color: #c33;
        }
        
        .alert-success {
            background-color: #efe;
            border: 1px solid #cfc;
            color: #3c3;
        }
        
        .loading {
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 2px solid #ffffff40;
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-right: 8px;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        .login-footer {
            padding: 20px 30px;
            background-color: #f8f9fa;
            text-align: center;
            font-size: 14px;
            color: #888;
        }
        
        .system-info {
            margin-top: 30px;
            padding: 20px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            text-align: center;
        }
        
        .default-accounts {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            padding: 16px;
            margin-top: 20px;
        }
        
        .default-accounts h4 {
            color: white;
            margin-bottom: 12px;
            font-size: 16px;
        }
        
        .account-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            color: rgba(255, 255, 255, 0.9);
            font-size: 14px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .account-item:last-child {
            border-bottom: none;
        }
        
        .account-role {
            background: rgba(255, 255, 255, 0.2);
            padding: 2px 8px;
            border-radius: 12px;
            font-size: 12px;
        }
        
        @media (max-width: 480px) {
            .login-container {
                margin: 10px;
            }
            
            .login-header {
                padding: 30px 20px;
            }
            
            .login-form {
                padding: 30px 20px;
            }
            
            .login-title {
                font-size: 24px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1 class="login-title">🏛️ 管理后台</h1>
            <p class="login-subtitle">南中医校史馆预约系统</p>
            
            <!-- 默认账户信息 -->
            <!--<div class="default-accounts">-->
            <!--    <h4>测试账户</h4>-->
            <!--    <div class="account-item">-->
            <!--        <span>admin / password</span>-->
            <!--        <span class="account-role">超级管理员</span>-->
            <!--    </div>-->
            <!--    <div class="account-item">-->
            <!--        <span>operator / password</span>-->
            <!--        <span class="account-role">操作员</span>-->
            <!--    </div>-->
            <!--    <div class="account-item">-->
            <!--        <span>viewer / password</span>-->
            <!--        <span class="account-role">查看员</span>-->
            <!--    </div>-->
            <!--</div>-->
        </div>
        
        <form class="login-form" id="loginForm">
            <div id="alertContainer"></div>
            
            <div class="form-group">
                <label class="form-label" for="username">用户名</label>
                <input 
                    type="text" 
                    id="username" 
                    name="username" 
                    class="form-input" 
                    placeholder="请输入用户名"
                    autocomplete="username"
                    required
                >
            </div>
            
            <div class="form-group">
                <label class="form-label" for="password">密码</label>
                <div class="password-input">
                    <input 
                        type="password" 
                        id="password" 
                        name="password" 
                        class="form-input" 
                        placeholder="请输入密码"
                        autocomplete="current-password"
                        required
                    >
                    <button type="button" class="password-toggle" onclick="togglePassword()">
                        👁️
                    </button>
                </div>
            </div>
            
            <div class="remember-me">
                <input type="checkbox" id="remember" name="remember" class="checkbox">
                <label for="remember" class="checkbox-label">记住登录状态</label>
            </div>
            
            <button type="submit" class="login-button" id="loginBtn">
                登录
            </button>
        </form>
        
        <div class="login-footer">
            <p>© 2025 南京中医药大学校史馆</p>
        </div>
    </div>

    <script>
        // API基础配置
        const API_BASE = '../api/';
        
        // 页面加载完成
        document.addEventListener('DOMContentLoaded', function() {
            // 检查是否已登录
            const token = localStorage.getItem('admin_token');
            if (token) {
                // 验证token有效性
                verifyToken(token);
            }
            
            // 绑定表单提交事件
            document.getElementById('loginForm').addEventListener('submit', handleLogin);
            
            // 绑定回车键登录
            document.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    handleLogin(e);
                }
            });
        });
        
        // 显示提示信息
        function showAlert(message, type = 'error') {
            const container = document.getElementById('alertContainer');
            container.innerHTML = `
                <div class="alert alert-${type}">
                    ${message}
                </div>
            `;
            
            // 3秒后自动清除
            setTimeout(() => {
                container.innerHTML = '';
            }, 3000);
        }
        
        // 切换密码显示
        function togglePassword() {
            const passwordInput = document.getElementById('password');
            const toggleBtn = document.querySelector('.password-toggle');
            
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                toggleBtn.textContent = '🙈';
            } else {
                passwordInput.type = 'password';
                toggleBtn.textContent = '👁️';
            }
        }
        
        // 处理登录
        async function handleLogin(event) {
            event.preventDefault();
            
            const loginBtn = document.getElementById('loginBtn');
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            const remember = document.getElementById('remember').checked;
            
            if (!username || !password) {
                showAlert('请输入用户名和密码');
                return;
            }
            
            // 设置加载状态
            loginBtn.disabled = true;
            loginBtn.innerHTML = '<span class="loading"></span>登录中...';
            
            try {
                const response = await fetch(API_BASE + 'admin_auth.php?action=login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });
                
                const data = await response.json();
                
                if (data.code === 200) {
                    // 登录成功
                    const { token, admin } = data.data;
                    
                    // 保存token和用户信息
                    if (remember) {
                        localStorage.setItem('admin_token', token);
                        localStorage.setItem('admin_info', JSON.stringify(admin));
                    } else {
                        sessionStorage.setItem('admin_token', token);
                        sessionStorage.setItem('admin_info', JSON.stringify(admin));
                    }
                    
                    showAlert('登录成功，正在跳转...', 'success');
                    
                    // 跳转到管理后台
                    setTimeout(() => {
                        window.location.href = 'index.html';
                    }, 1000);
                    
                } else {
                    showAlert(data.message || '登录失败');
                }
                
            } catch (error) {
                console.error('登录请求失败:', error);
                showAlert('网络请求失败，请检查网络连接');
            } finally {
                // 恢复按钮状态
                loginBtn.disabled = false;
                loginBtn.innerHTML = '登录';
            }
        }
        
        // 验证token有效性
        async function verifyToken(token) {
            try {
                const response = await fetch(API_BASE + 'admin_auth.php?action=profile', {
                    method: 'GET',
                    headers: {
                        'Authorization': 'Bearer ' + token
                    }
                });
                
                const data = await response.json();
                
                if (data.code === 200) {
                    // token有效，直接跳转
                    window.location.href = 'index.html';
                } else {
                    // token无效，清除存储
                    localStorage.removeItem('admin_token');
                    localStorage.removeItem('admin_info');
                    sessionStorage.removeItem('admin_token');
                    sessionStorage.removeItem('admin_info');
                }
            } catch (error) {
                console.error('Token验证失败:', error);
            }
        }
        
        // 快速填充测试账户（点击账户信息时）
        document.addEventListener('click', function(e) {
            const accountItem = e.target.closest('.account-item');
            if (accountItem) {
                const accountText = accountItem.textContent;
                const [username, password] = accountText.split(' / ')[0].split(' ');
                
                if (username && password === 'password') {
                    document.getElementById('username').value = username;
                    document.getElementById('password').value = 'password';
                    showAlert('已填充测试账户信息', 'success');
                }
            }
        });
    </script>
</body>
</html>